<template>
  <div class="recommend" ref="recommend">
    <scroll ref="scroll" class="recommend-content" :data="getdata">
      <div>
        <div v-if="recommends.length" class="slider-wrapper" ref="sliderWrapper">
          <slider>
            <div v-for="(item,index) in recommends" :key="index">
              <a :href="item.pic">
                <img class="needsclick" @load="loadImage" :src="item.pic">
              </a>
            </div>
          </slider>
        </div>
           <div class="recommned-song">
          <h1 class="song-title" v-if="discList.length>0">热门歌单推荐</h1>
          <ul>
            <li v-for="(item,index) in discList" :key="index" class="item" @click="selectItem(item)">
                <div class="pic">
                   <img v-lazy="item.picUrl" >
                </div>
                <div class="text">
                  <span v-html="item.name"></span>
                </div>
            </li>
          </ul>
        </div>
        <div class="recommend-list">
          <h1 class="list-title" v-if="songList.length>0">热门歌曲推荐</h1>
          <ul>
            <li @click="selectHot(item,index)" v-for="(item,index) in songList" :key="index" class="item">
              <div class="icon">
                <img width="60" height="60" v-lazy="item.album.blurPicUrl">
              </div>
              <div class="text">
                <h2 class="name" v-html="item.name"></h2>
                <p class="desc" v-html="item.artists[0].name"></p>
              </div>
            </li>
          </ul>
        </div>
      </div>
      <div class="loading-container" v-show="!discList.length">
        <loading></loading>
      </div>
    </scroll>
    <router-view></router-view>
  </div>
</template>

<script>
import { getRecommend, getAuth, getDiscList, getDiscSong,getBnaner } from 'api/recommend'
import Scroll from 'base/scroll/scroll'
import Loading from 'base/loading/loading'
import Slider from 'base/slider/slider'
import doPost from 'common/js/xhr.js'
import {playlistMixin} from 'common/js/mixin'
import {songDetail, getUrl,checkMusic} from 'api/singer'
import {mapMutations,mapActions} from 'vuex'

export default {
  mixins: [playlistMixin],
  created () {
    this._getRecommend();
    this._getDiscList();
    this._getDiscSong();
  },
  components: {
    Slider,
    Scroll,
    Loading
  },
  data () {
    return {
      recommends: [],
      discList: [],
      songList: []
    }
  },
  computed: {
    getdata () {
      if(this.discList.length&&this.songList.length) {
        return this.songList.concat(this.discList)
      }
    }
  },
  methods: {
    handlePlaylist(playlist) {
      const bottom = playlist.length > 0 ? '60px' : ''
      this.$refs.recommend.style.bottom = bottom
      this.$refs.scroll.refresh()
    },
    _getDiscList () {
      let _this = this
      getAuth().then(function(res){
        if(res) {
          console.log('res',res)
          let cookie = res.data.cookie;
          console.log('cookie',cookie)
          let data = res.data.body;
          document.cookie = cookie.toString();
          console.log( document.cookie)
          if(res.status == 200) {
            getDiscList().then( res => {
              _this.discList = res.data.body.recommend
            })
          }
        }
      })
    },
    _getDiscSong() {
      let _this = this
      getDiscSong().then(res => {
        console.log(res)
         _this.songList = res.data.body.recommend
      })
    },
    _getRecommend() {
      getBnaner().then(res => {
        console.log(res)
        this.recommends = res.data.body.banners
        console.log(this.recommends )
      })
    },
    selectItem (item) {
      this.$router.push({
        path: `/recommend/${item.id}`
      })
      this.setDic(item)
    },
    selectHot(item,index){
      this.insertSong(item)
      //选中当前热门歌曲 全局设置歌曲（vuex）
      // let id = item.id
      // let song = [item]
      // this.selectPlay({
      //   list: song,
      //   index: 0
      // })
    },
    loadImage() {
      if (!this.checkloaded) {
        this.checkloaded = true
        this.$refs.scroll.refresh()
      }
    },
    ...mapActions([
        'selectPlay',
        'insertSong'
    ]),
    ...mapMutations({
      setDic: 'SET_DISC',
    })
  }
}
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  @import "~common/stylus/mixin"
  @import "~common/stylus/variable"

  .recommend
    position: fixed
    width: 100%
    top: 88px
    bottom: 0
    background-color: #2c2c2c
    .recommend-content
      height: 100%
      overflow: hidden
      .slider-wrapper
        position: relative;
        width: 95%;
        overflow: hidden;
        border-radius: 10px;
        margin: 10px auto;
        img
          width 100%
          height 100%
      .recommend-list,.recommned-song
        .list-title,.song-title
          height: 65px
          line-height: 65px
          text-align center
          font-size: $font-size-medium-x
          color: $color-h
      .recommend-list
        .item
          display: flex
          box-sizing: border-box
          align-items: center
          padding: 0 20px 20px 20px
          .icon
            flex: 0 0 60px
            width: 60px
            padding-right: 20px
          .text
            display: flex
            flex-direction: column
            justify-content: center
            flex: 1
            line-height: 20px
            overflow: hidden
            font-size: $font-size-medium
            .name
              margin-bottom: 10px
              color: $color-text
            .desc
              color: $color-text-d
      .recommned-song
        ul
        &::after
          display block
          overflow hidden
          clear both
        .item
          width 33.33%
          float left
          text-align center
          .pic  img
              width 100px
              height 100px
              border-radius 3px
          .text
            display block
            width 80%
            margin 10px auto
            font-size 12px
            span
              display block
              no-wrap()
      .loading-container
        position: absolute
        width: 100%
        top: 50%
        transform: translateY(-50%)
</style>
